﻿using BCCommon;
using BCData.TMS.MoveHouseConfig.MoveHouseServiceConfig;
using BCData.TMS.MoveHouseConfig.MoveHouseSpecificationServiceConfig;
using BCDto.Sim.Admin.Admin;
using BCDto.TMS.MoveHouseConfig.MoveHouseServiceConfig;
using BCDto.TMS.MoveHouseConfig.MoveHouseServiceConfig.MoveHouseServiceConfigRequest;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.MoveHouseConfig.MoveHouseServiceConfig;
using BCService.Utils;
using System;
using System.Collections.Generic;

namespace BCService.TMS.MoveHouseConfig.MoveHouseServiceConfig
{
    public class MoveHouseServiceConfigService : IMoveHouseServiceConfigService
    {
        private readonly IMoveHouseServiceConfigData moveHouseServiceConfigData;
        private readonly IMoveHouseSpecificationServiceConfigData moveHouseSpecificationServiceConfigData;
        private readonly IRedisService redisService;
        private readonly string key = string.Format("{0}", EntityNameConst.MoveHouseServiceConfigEntity);
        public MoveHouseServiceConfigService(IRedisService redisService,
            IMoveHouseServiceConfigData moveHouseServiceConfigData,
            IMoveHouseSpecificationServiceConfigData moveHouseSpecificationServiceConfigData)
        {
            this.moveHouseServiceConfigData = moveHouseServiceConfigData;
            this.redisService = redisService;
            this.moveHouseSpecificationServiceConfigData = moveHouseSpecificationServiceConfigData;
        }

        public MoveHouseServiceConfigDto Create(MoveHouseServiceConfigRequestDto requestDto, AdminDto admin)
        {
            var isExist = moveHouseServiceConfigData.IsExist(requestDto.ServiceName, null).GetAwaiter().GetResult();
            if (isExist)
            {
                throw new ArgumentException(" 当前服务名称已存在,不可重复添加 ");
            }
            var entity = new MoveHouseServiceConfigEntity()
            {
                ServiceName = requestDto.ServiceName,
                Remark = requestDto.Remark,
                Enabled = true,
                CreateTime = DateTime.Now,
                CreateAdminId = admin.AdminId,
                CreateAdminName = admin.FullName,
                UpdateTime = DateTime.Now,
                EditAdminId = admin.AdminId,
                EditAdminName = admin.FullName,
            };
            var result = this.moveHouseServiceConfigData.InsertAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<MoveHouseServiceConfigDto>();
        }

        public MoveHouseServiceConfigDto Update(long moveHouseServiceConfigId, MoveHouseServiceConfigRequestDto requestDto, AdminDto admin)
        {
            var entity = moveHouseServiceConfigData.GetSingle(moveHouseServiceConfigId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取搬家服务配置失败 ");
            }
            var isExist = moveHouseServiceConfigData.IsExist(requestDto.ServiceName, moveHouseServiceConfigId).GetAwaiter().GetResult();
            if (isExist)
            {
                throw new ArgumentException(" 当前服务名称已存在,不可修改 ");
            }
            entity.ServiceName = requestDto.ServiceName;
            entity.Remark = requestDto.Remark;
            entity.UpdateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            var result = this.moveHouseServiceConfigData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<MoveHouseServiceConfigDto>();
        }
        public MoveHouseServiceConfigDto UpdateEnabled(long moveHouseServiceConfigId, bool isEnabled, AdminDto admin)
        {
            var entity = moveHouseServiceConfigData.GetSingle(moveHouseServiceConfigId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取搬家服务配置失败 ");
            }
            entity.Enabled = isEnabled;
            entity.UpdateTime = DateTime.Now;
            entity.EditAdminId = admin.AdminId;
            entity.EditAdminName = admin.FullName;
            var result = this.moveHouseServiceConfigData.UpdateAsync(entity).GetAwaiter().GetResult();
            this.redisService.DeleteMultipleKey(key, false);
            return result.As<MoveHouseServiceConfigDto>();
        }

        public bool Delete(long moveHouseServiceConfigId)
        {
            var entity = moveHouseServiceConfigData.GetSingle(moveHouseServiceConfigId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取搬家服务配置失败 ");
            }
            var isExist = moveHouseSpecificationServiceConfigData.IsExistService(moveHouseServiceConfigId).Result;
            if (isExist > 0)
            {
                throw new ArgumentException(" 当前搬家服务已经被使用,不可删除 ");
            }
            var result = this.moveHouseServiceConfigData.DeleteAsync(moveHouseServiceConfigId).GetAwaiter().GetResult();
            if (result > 0)
            {
                this.redisService.DeleteMultipleKey(key, false);
            }
            return true;
        }
        public IEnumerable<MoveHouseServiceConfigDto> GetList(bool? isEnabled)
        {
            string cacheKey = this.redisService.GetKey(key, isEnabled);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var entity = this.moveHouseServiceConfigData.GetList(isEnabled).Result;
                return entity;
            }, TimeSpan.FromDays(1));
            return result.As<IEnumerable<MoveHouseServiceConfigDto>>();
        }

        public PageableList<MoveHouseServiceConfigDto> Query(bool? isEnabled, string serviceName, DateTime? createStartDate,
             DateTime? createEndDate, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, isEnabled, serviceName, createStartDate, createEndDate, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var (list, count) = this.moveHouseServiceConfigData.Query(isEnabled, serviceName, createStartDate, createEndDate,
                    pageIndex, pageSize).Result;
                return new PageableList<MoveHouseServiceConfigDto>
                {
                    Items = list.As<IEnumerable<MoveHouseServiceConfigDto>>(),
                    Count = count,
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now,
                };
            }, TimeSpan.FromDays(1));
            return result;
        }

        public MoveHouseServiceConfigDto Enabled(long moveHouseServiceConfigId, AdminDto admin)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseServiceConfigData.GetSingle(moveHouseServiceConfigId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取搬家服务配置失败 ");
            }
            entity.Enabled = true;
            var result = moveHouseServiceConfigData.UpdateAsync(entity).GetAwaiter().GetResult();
            if (result != null)
            {
                this.redisService.DeleteMultipleKey(key, false);
            }
            return result.As<MoveHouseServiceConfigDto>();
        }

        public MoveHouseServiceConfigDto Disabled(long moveHouseServiceConfigId, AdminDto admin)
        {
            if (admin.CompanyId != 1)
            {
                throw new ArgumentException(" 用户认证失败,错误操作 ");
            }
            var entity = moveHouseServiceConfigData.GetSingle(moveHouseServiceConfigId).Result;
            if (entity == null)
            {
                throw new ArgumentException(" 获取搬家服务配置失败 ");
            }
            var isExist = moveHouseSpecificationServiceConfigData.IsExistService(moveHouseServiceConfigId).Result;
            if (isExist > 0)
            {
                throw new ArgumentException(" 当前搬家服务已经被使用,不可禁用 ");
            }
            entity.Enabled = false;
            var result = moveHouseServiceConfigData.UpdateAsync(entity).GetAwaiter().GetResult();
            if (result != null)
            {
                this.redisService.DeleteMultipleKey(key, false);
            }
            return result.As<MoveHouseServiceConfigDto>();
        }
    }
}
